package com.jjh.common.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.jjh.common.pojo.InfoEnum;
import com.jjh.common.util.ConstatFinalUtil;
import com.jjh.common.util.DateUtil;
import com.jjh.common.util.EncryptUtil;
import com.jjh.common.util.PageInfoUtil;
import com.jjh.common.util.RedisUtil;
import com.jjh.common.util.RegexUtil;

/**
 * 所有Servlet公共的代码
 * 
 * @author Zjx
 */
@Controller
public class BaseServlet
{
	
	/**
	 * 提示信息
	 */
	protected String info = "" ; 
	
	/**
	 * 公共类
	 */
	protected RegexUtil regexUtil = new RegexUtil();
	protected DateUtil dateUtil = new DateUtil() ;
	protected EncryptUtil enuEncryptUtil = new EncryptUtil();
	@Autowired
	protected RedisUtil redisUtil ;

	
	/**
	 * 白页面
	 * @throws IOException 
	 */
	protected void blank(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		/* 错误信息 */
		String info = ConstatFinalUtil.INFO_JSON.getString(InfoEnum.INFO_ERROR_BLANK.getCode() + "") ;
		this.printStr(request, response, info);
	}
	
	/**
	 * 输出字符串给浏览器
	 * @throws IOException 
	 */
	protected void printStr(HttpServletRequest request,HttpServletResponse response,
			String info) throws IOException
	{
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		/* 发送信息 */
		out.println(info);
		out.flush();
		out.close();
	}
	
	/**
	 * 生成分页工具类的方法
	 * @param request
	 * @return
	 */
	protected PageInfoUtil proccedPageInfoUtil(HttpServletRequest request)
	{
		PageInfoUtil pageInfoUtil = new PageInfoUtil();
		
		/* 接收参数 */
		String currentPage = request.getParameter("currentPage");
		String pageSize = request.getParameter("pageSize");
		try
		{
			/* 设置分页参数 */
			pageInfoUtil.setCurrentPage(Integer.parseInt(currentPage));
			pageInfoUtil.setPageSize(Integer.parseInt(pageSize));
		} catch (NumberFormatException e)
		{
		}
		return pageInfoUtil;
	}
	
	
	/**
	 * 生成搜索条件
	 * @param request
	 * @return
	 */
	protected Map<String, Object> proccedSearchCond(HttpServletRequest request)
	{
		Map<String,Object> condMap = new HashMap<String,Object>();
		/* ==关键字处理开始== */
		String keyword = request.getParameter("keyword");
		if(keyword == null)
		{
			keyword = "" ; 
		}
		keyword = keyword.trim() ; 
		condMap.put("keyword", keyword);
		request.setAttribute("keyword", keyword);
		/* ==关键字处理结束== */
		/* ==状态处理开始== */
		String status = request.getParameter("status");
		if(status == null)
		{
			status = "" ; 
		}
		condMap.put("status", status);
		request.setAttribute("status", status);
		/* ==状态处理结束== */
		
		/* ==时间处理开始== */
		String st = request.getParameter("st");
		if(st == null)
		{
			st = "" ; 
		}
		String ed = request.getParameter("ed");
		if(ed == null)
		{
			ed = "" ; 
		}
		
		Date stDate = null ; 
		Date edDate = null ; 
		if(!"".equalsIgnoreCase(st) && !"".equalsIgnoreCase(ed))
		{
			/* 说明传值了,将字符串转换为时间 */
			stDate = this.dateUtil.strToDateTime(st);
			edDate = this.dateUtil.strToDateTime(ed);
		}
		condMap.put("st", stDate);
		condMap.put("ed", edDate);
		request.setAttribute("st", st);
		request.setAttribute("ed", ed);
		/* ==时间处理结束== */
		return condMap;
	}
}
